<!-- 商品录入 -->
<template>
  <el-container>
    <el-aside width="170px">
      <CategoryManager :config="categoryConfig" />
    </el-aside>
    <el-main class="base_main_content">
      <div class="base_screen_header">
        <el-button type="primary" @click="add">新增商品</el-button>
        <el-form :inline="true" class="base_search_form" @submit.prevent>
          <el-form-item>
            <el-select v-model="base" @change="searchBase" placeholder="请选择基地" clearable>
              <el-option v-for="(item, index) in baseOptions" :key="index" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="name" @keyup[native].enter="searchName" @change="searchName" placeholder="商品名称" clearable
              :suffix-icon="Search" />
          </el-form-item>
        </el-form>
      </div>
      <StatusBar :data="statusList" default="1" />
      <div class="base_main_header">
        <el-button type="primary" text @click="goodsExport">导出</el-button>
      </div>
      <GoodsTable type="input" route="goodsinput" @statusData="statusData" />
    </el-main>
  </el-container>
</template>

<script setup>
import GoodsTable from '@com/goods/GoodsTable.vue'
import CategoryManager from '@com/CategoryManager.vue';
import StatusBar from '@com/StatusBar.vue';
import { ref, reactive } from 'vue'
import { Search } from '@element-plus/icons-vue'
import useRoute from "@util/router";
import goodsConfigApi from "@api/goodsConfig"
import api from "@api/goods"
import $ from "@util/base"

const route = useRoute()

const categoryConfig = {
  type: 'goods'
}

const base = ref(route.query().base || '')
const name = ref(route.query().name || '')

const baseOptions = ref([])

getConfig()

function getConfig() {
  goodsConfigApi.base(val => {
    baseOptions.value = val
  })
}

const statusList = reactive([{
  label: '全部',
  value: '1',
  total: 0
}, {
  label: '草稿',
  value: '1007001',
  total: 0
}, {
  label: '待审批',
  value: '1007002',
  total: 0
}, {
  label: '已通过',
  value: '1007004',
  total: 0
}, {
  label: '已驳回',
  value: '1007003',
  total: 0
}])

function statusData(val) {
  statusList.forEach((e, i) => { e.total = val[i] })
}

function searchBase(val) {
  route.replaceQuery('base', val)
}

function searchName(val) {
  route.replaceQuery('name', val)
}

function add() {
  route.push({ name: 'goodsdetails', query: { type: 'add' } })
}

function goodsExport() {
  let routeQuery = route.query()
  api.inputExport({
    categoryCode: routeQuery.c,
    shopCode: routeQuery.base,
    productName: routeQuery.name,
    state: routeQuery.status,
  }, val => {
    $.download(val.url, "商品录入列表导出")
  })
}
</script>

<style lang="scss">

</style>
